//Blue Tails' moveset

freeslot("MT_THETAILSTAILS", "S_THETAILSTAILS", "SPR_TAIL")

mobjinfo[MT_THETAILSTAILS] = {
	spawnstate = S_THETAILSTAILS,
	radius = 10*FRACUNIT,
	height = 22*FRACUNIT,
	flags = MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY|MF_SOLID
}

states[S_THETAILSTAILS] = {
    tics = 37,
	sprite = SPR_TAIL,
	frame = A,
    nextstate = S_NULL
}

sfxinfo[sfx_s1c9].caption = "Dropped tails"

addHook("PlayerThink", function(player)
	if player.mo and player.mo.valid
		if player.tailsflyrun == nil
			player.tailsflyrun = 0
		end
		if player.tailsflyrun > 0
		and not P_IsObjectOnGround(player.mo)
			player.tailsflyrun = $-1
			player.mo.flags = $|MF_NOGRAVITY
			if (leveltime % 10) == 1
				S_StartSound(player.mo, sfx_putput)
			end
		end
		if player.tailstintblue == true
		and player.isblue == true
			if player.mo.state == S_PLAY_RUN
			and player.speed >= player.runspeed
			and P_IsObjectOnGround(player.mo)
			and not player.tailsflyrun
				player.mo.flags = $|MF_NOGRAVITY
				player.tailsflyrun = 2*TICRATE
			end
			if player.mo.flags & MF_NOGRAVITY
				if player.mo.state != S_PLAY_RUN
				or player.speed < player.runspeed
				or not player.tailsflyrun
					if not P_IsObjectOnGround(player.mo)
					and not (player.pflags & PF_JUMPED)
						P_DoJump(player, true)
						player.pflags = $ & ~PF_JUMPED
						player.mo.state = S_PLAY_SPRING
					end
					player.mo.flags = $ & ~MF_NOGRAVITY
					player.tailsflyrun = 0
				end
			end
		end
		if player.tailstintblue == true
		and player.isblue == true
		and player.mo.state == S_PLAY_FLY or player.mo.state == S_PLAY_FLY_TIRED
			if player.cmd.buttons & BT_USE
				player.pflags = $|PF_JUMPED
				player.mo.state = S_PLAY_ROLL
				S_StartSound(player.mo, sfx_spndsh, player)
				player.powers[pw_tailsfly] = 0
			end
		end
		if player.tailscanshoot == nil
			player.tailscanshoot = true
		end
		if player.tailstintblue == true
		and player.isblue == true
		and player.playerstate == PST_LIVE
		and player.followmobj and player.followmobj.valid
			if P_IsValidSprite2(player.mo, SPR2_TAL0)
			and P_IsValidSprite2(player.mo, SPR2_TAL1)
			and P_IsValidSprite2(player.mo, SPR2_TAL2)
			and P_IsValidSprite2(player.mo, SPR2_TAL3)
			and P_IsValidSprite2(player.mo, SPR2_TAL4)
			and P_IsValidSprite2(player.mo, SPR2_TAL5)
			and P_IsValidSprite2(player.mo, SPR2_TAL6)
			and P_IsValidSprite2(player.mo, SPR2_TAL7)
			and P_IsValidSprite2(player.mo, SPR2_TAL8)
			and P_IsValidSprite2(player.mo, SPR2_TAL9)
			and P_IsValidSprite2(player.mo, SPR2_TALA)
			and P_IsValidSprite2(player.mo, SPR2_TALB)
				if player.cmd.buttons & BT_CUSTOM1
				and player.tailscanshoot == true
					local tailsprojectile = P_SpawnMobjFromMobj(player.mo, 0, 0, 0, MT_THETAILSTAILS)
					tailsprojectile.target = player.mo
					tailsprojectile.color = player.mo.color
					S_StartSound(player.mo, sfx_s1c9, player)
					player.tailsprojectile = tailsprojectile
					player.tailscanshoot = false
				end
				if player.tailscanshoot == false
					if player.tailsprojectile and player.tailsprojectile.valid
						player.drawangle = $+ANG10
						player.mo.state = S_PLAY_EDGE
						P_InstaThrust(player.mo, 0, 0)
						player.powers[pw_nocontrol] = 2
						player.followmobj.flags2 = $|MF2_DONTDRAW
						P_InstaThrust(player.tailsprojectile, player.drawangle-ANG10, 20*FRACUNIT)
						player.tailsprojectile.sprite = SPR_TAIL
					else
						player.followmobj.flags2 = $ & ~MF2_DONTDRAW
						player.tailscanshoot = true
					end
				end
			end
		end
	end
end)

addHook("MobjCollide", function(mobj, mo)
	if mobj and mobj.valid
	and mobj.target and mobj.target.valid
	and mo and mo.valid
		if P_IsValidSprite2(mobj.target, SPR2_TAL0)
		and P_IsValidSprite2(mobj.target, SPR2_TAL1)
		and P_IsValidSprite2(mobj.target, SPR2_TAL2)
		and P_IsValidSprite2(mobj.target, SPR2_TAL3)
		and P_IsValidSprite2(mobj.target, SPR2_TAL4)
		and P_IsValidSprite2(mobj.target, SPR2_TAL5)
		and P_IsValidSprite2(mobj.target, SPR2_TAL6)
		and P_IsValidSprite2(mobj.target, SPR2_TAL7)
		and P_IsValidSprite2(mobj.target, SPR2_TAL8)
		and P_IsValidSprite2(mobj.target, SPR2_TAL9)
		and P_IsValidSprite2(mobj.target, SPR2_TALA)
		and P_IsValidSprite2(mobj.target, SPR2_TALB)
		and mobj.target.player.followmobj and mobj.target.player.followmobj.valid
		and mobj.target.player.isblue == true
			mobj.angle = leveltime*(-ANG60)
			if (leveltime % 10) == 1
				S_StartSound(mobj, sfx_putput)
			end
			if mobj.target != mo
			and mobj.z >= mo.z
			and mobj.z <= mo.z+mo.height
				if mo.type == MT_PLAYER
					P_DoPlayerPain(mo.player, mobj.target, mobj)
					P_RemoveMobj(mobj)
				elseif mo.flags & MF_ENEMY
					P_DamageMobj(mo, mobj.target, mobj)
					P_RemoveMobj(mobj)
				end
			end
		else
			mobj.tics = 40
			mobj.angle = leveltime*(-ANG60)
			mobj.momz = 2*FRACUNIT*P_MobjFlip(mobj)
			if (leveltime % 10) == 1
				S_StartSound(mobj, sfx_putput)
			end
		end
	end
end, MT_THETAILSTAILS)